package com.hrt.datasender.process.strategy.hj212;

import com.btcode.db.result.Row;
import com.btcode.orm.conver.DefaultModelConver;
import com.hrt.datasender.datacollect.drive.weigeng.BdDoorAccessModel;
import com.hrt.datasender.datadao.model.BcSendPlatformModel;
import com.hrt.datasender.datadao.model.BdDeviceStateModel;
import com.hrt.datasender.datadao.service.BCSiteService;
import com.hrt.datasender.process.indentity.hj212.MessageModel;
import com.hrt.datasender.process.strategy.IProcessStrategy;
import com.hrt.datasender.process.strategy.hj212.util.CRCUtil;

import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ComA001Process implements IProcessStrategy {

    @Override
    public String getStrategyName() {
        return "站房状态数据解析";
    }

    @Override
    public String getCommandId() {
        return "A001";
    }

    @Override
    public Object packet(List<Row> data, BcSendPlatformModel platform) throws Exception {
        String result = getCommandPacket(platform, data);
        return result;
    }

    @Override
    public Object packetReissue(List<Row> data, BcSendPlatformModel platform) throws Exception {
        String result = getCommandPacket(platform, data);
        return result;
    }

    @Override
    public void decode(MessageModel data, BcSendPlatformModel sendPlatform) throws Exception {
        // TODO Auto-generated method stub

    }

    public String getCommandPacket(BcSendPlatformModel sendPlatform, List<Row> data) {
        StringBuffer msgBuf = new StringBuffer();
        SimpleDateFormat sdfDT = new SimpleDateFormat("yyyyMMddHHmmss");
        msgBuf.append("QN=").append(sdfDT.format(data.get(0).getSafeString("DATATIME"))).append("000;ST=").append(BCSiteService.getInstance().getModel().getSystemType())
            .append(";CN=").append(getCommandId()).append(";PW=123456;MN=").append(sendPlatform.getMn())
            .append(";Flag=1;" + "CP=&&DataTime=").append(sdfDT.format(data.get(0).getSafeString("DATATIME"))).append(";Calibratetime=")
            .append(sdfDT.format(data.get(0).getSafeString("CALIBRATETIME"))).append(";");
        for (Row row : data) {
            msgBuf.append(row.getSafeString("DEVICE")).append("-alarm=").append(row.getSafeString("ALARM")).append(",")
                .append(row.getSafeString("DEVICE")).append("-error=").append(row.getSafeString("ERROR")).append(",")
                .append(row.getSafeString("DEVICE")).append("-status=").append(row.getSafeString("STATUS")).append(",")
                .append(row.getSafeString("DEVICE")).append("-absorbance=").append(row.getSafeString("CALIBRATETIME")).append(";");
        }
        msgBuf.append("&&");
        String crc = CRCUtil.calcCRC(msgBuf.toString());
        msgBuf.insert(0, "##" + getNf().format(msgBuf.length())).append(crc).append("\r\n");
        return msgBuf.toString();
    }

    private NumberFormat nf;

    private NumberFormat getNf() {
        if (nf == null) {
            nf = NumberFormat.getInstance();
            nf.setGroupingUsed(false);
            nf.setMaximumIntegerDigits(4);
            nf.setMinimumIntegerDigits(4);
        }
        return nf;
    }
}
